package winOrDie.GameEngine.RT.Server_Engine;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.EventObject;

public class WoD_RT_Server_Engine_Bucket_Event extends EventObject implements Comparable<WoD_RT_Server_Engine_Bucket_Event> {
	
	/*
	 * Movement data
	 */
	private int type = -1;
	private long Ts = 0L;
	private int estadoPartida = 0; //0 -> empezada, 1 -> princesa rescatada de la caseta, 2 -> capitan & princesa ganan, 3 -> piratas ganan
	private int coordY = 0;
	private int coordX = 0;
	private int Player = 0;
	
	private Object data = null;
	
	/*
	 * Tipos de evento
	 */
	public static final int MOVE = 0;
	
	
	
	
	public long getTs() {
		return Ts;
	}

	public void setTs(long ts) {
		Ts = ts;
	}
	
	/**
	 * 0 -> empezada, 1 -> princesa rescatada de la caseta, 2 -> capitan & princesa ganan, 3 -> piratas ganan
	 * @return
	 */
	public int getEstadoPartida() {
		return estadoPartida;
	}

	/**
	 * 0 -> empezada, 1 -> princesa rescatada de la caseta, 2 -> capitan & princesa ganan, 3 -> piratas ganan
	 * @param estado
	 */
	public void setEstadoPartida(int estado) {
		this.estadoPartida = estado;
	}

	public int getCoordX() {
		return coordX;
	}

	public void setCoordX(int coordX) {
		this.coordX = coordX;
	}

	public int getCoordY() {
		return coordY;
	}

	public void setCoordY(int coordY) {
		this.coordY = coordY;
	}
	
	public int getType() {
		return type;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public int getPlayer() {
		return Player;
	}

	public void setPlayer(int player) {
		Player = player;
	}


	/**
	 * 
	 * @param source
	 * @param type
	 */
	public WoD_RT_Server_Engine_Bucket_Event(Object source, int type) {
		super(source);
		this.type = type;
	}
	
	@Override
	public int compareTo(WoD_RT_Server_Engine_Bucket_Event o) {
		if (Ts < o.getTs())
		{
			return -1;
		}
		else if (Ts > o.getTs())
		{
			return 1;
		}
		
		return 0;
	}

	
	public byte[] get_BytesArray() throws IOException
	{
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
		
		dataOutputStream.writeByte(type);
		
		switch (type) {
		
		/**
		 * Move event
		 */
		case MOVE:
			dataOutputStream.writeLong(Ts);
			dataOutputStream.writeByte(estadoPartida);
			dataOutputStream.writeByte(Player);
			dataOutputStream.writeInt(coordX);
			dataOutputStream.writeInt(coordY);

			break;


		
				
		default:
			break;
		}
		
	
		
		
		return byteArrayOutputStream.toByteArray();
	}
	
	public static WoD_RT_Server_Engine_Bucket_Event get_Pong_Event_From_ByteArray(byte[] buffer) throws IOException
	{
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
		DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
		
		int type = dataInputStream.readByte();
		
		WoD_RT_Server_Engine_Bucket_Event event = new WoD_RT_Server_Engine_Bucket_Event("a", type);
		
		switch (type) {
		case MOVE:
			event.setTs(dataInputStream.readLong());
			event.setEstadoPartida(dataInputStream.readByte());
			event.setPlayer(dataInputStream.readByte());
			event.setCoordY(dataInputStream.readInt());

			break;
		
		
		default:
			break;
		}
		
		
		
		/*
		// Old method
		event.setTs(dataInputStream.readLong());
		event.setPlayer(dataInputStream.readByte());
		event.setCoordX(dataInputStream.readInt());
		event.setCoordY(dataInputStream.readInt());		
		 */
		
		return event;
	}
	
}
